package occ.utils
{
  
	import flash.data.*;
	import flash.display.*;
	import flash.events.*;
	import flash.filesystem.*;
	
	import mx.controls.*;
	import mx.core.*;
	
  public class DatabaseManager extends EventDispatcher
  {
     public static const IS_INIT:String="DataBaseManagerIsInitialized";
     private static var dbconn:SQLConnection;
		
	  public function init():void
		{
		  if(dbconn == null){
		    dbconn = new SQLConnection();
  			dbconn.addEventListener( SQLErrorEvent.ERROR, onSQLError );
			var dbFile:File = new File(File.applicationDirectory.nativePath + "/occ");
  			if(dbFile.exists){
  				 dbconn.addEventListener( SQLEvent.OPEN, onSQLOpen );
  				 dbconn.openAsync( dbFile );
  				 trace("Database was found");
  			}else{
  				trace("Database not found");
  			}
		  }
		}
		
		public static function getConnection():SQLConnection
		{
		  if(dbconn != null)
		   return dbconn;
		  else
		    throw new Error("Database not initiliazed");
		}
		
		private  function onSQLError( event:SQLErrorEvent ):void
		{
			 var errorString:String = "SQL Error: " + event.error.errorID + "nDetails: " + event.error.details + "nDetail Arguments: " +event.error.detailArguments;
 			Alert.show( errorString );
		}
		
		public function onSQLOpen( event:SQLEvent ):void
		{ 
		 trace("He is open");
		 //callBackFunc();
		 dispatchEvent(new Event(IS_INIT));
		 
		 // Alert.show( "Database was opened successfully." );
		}

  }
}